/**
 * 防抖指令
 * @param {*} el
 * @param {*} binding
 */
export default {
  name: `debounce`,
  mounted(el, binding) {
    const { time, fn } = binding.value || { time: 1000, fn: null }
    let timeoutId = null

    el.addEventListener(`click`, () => {
      if (fn && !el.disabled) {
        el.disabled = true

        if (timeoutId) {
          clearTimeout(timeoutId)
        }

        timeoutId = setTimeout(() => {
          el.disabled = false
          timeoutId = null
        }, time)

        fn() // 调用传递的函数
      }
    })
  },
}
